﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using Microsoft.Maps.MapControl;
using System.Windows.Media.Imaging;
using System.IO;

namespace imaginecup
{
    /// <summary>
    /// 注意服务器的路径，上传图片是使用一个httpHandler
    /// 在不同部署下可能需要重新修改
    /// </summary>
    public partial class AddRecord : UserControl
    {
        private OpenFileDialog openFile = new OpenFileDialog();
        private AddRecordWin childwin;
        private ServiceReference1.DataProviderSoapClient addRecordClient = new imaginecup.ServiceReference1.DataProviderSoapClient();
        private bool imageupload = false;
        private string uploadservice = "http://localhost:1276/UpLoadImage.ashx?filename=";
        
        public AddRecord()
        {
            InitializeComponent();
            btnAddRecord.Click +=new RoutedEventHandler(btnAddRecord_Click);
        }

        public AddRecord(Location l,AddRecordWin win)
        {
            InitializeComponent();
            childwin = win;
            dllongtitude.Content = l.Longitude;
            dllatitude.Content = l.Latitude;
            btnAddRecord.Click += new RoutedEventHandler(btnAddRecord_Click);
            btnChoosePhoto.Click += new RoutedEventHandler(btnChoosePhoto_Click);
        }

        void btnChoosePhoto_Click(object sender, RoutedEventArgs e)
        {
            if (openFile.ShowDialog() == true)
            {
                BitmapImage bmp = new BitmapImage();
                bmp.SetSource(openFile.File.OpenRead());
                imagepreview.Source = bmp;
                imageupload = true;
            }
        }
        /// <summary>
        /// 将数据上传到服务器，同时关闭此窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btnAddRecord_Click(object sender, RoutedEventArgs e)
        {
            ServiceReference1.CPollutedRecord tmpr = new imaginecup.ServiceReference1.CPollutedRecord();

            tmpr.Latitude = Convert.ToDouble(dllatitude.Content.ToString());
            tmpr.Longtitude = Convert.ToDouble(dllongtitude.Content.ToString());
            tmpr.Name = tbName.Text;
            tmpr.Comments = tbComments.Text;
            tmpr.PostTime = DateTime.Now;
            tmpr.Rank_0 = dlr0.Value/(dlr0.Maximum-dlr0.Minimum);
            tmpr.Rank_1 = dlr1.Value/(dlr1.Maximum-dlr1.Minimum);
            tmpr.Rank_2 = dlr2.Value/(dlr2.Maximum-dlr2.Minimum);
            tmpr.Rank_3 = dlr3.Value/(dlr3.Maximum-dlr3.Minimum);
            tmpr.Rank_4 = dlr4.Value/(dlr4.Maximum-dlr4.Minimum);
            if (imageupload)
                tmpr.Images = tmpr.Name + tmpr.PostTime.Year + tmpr.PostTime.Month + tmpr.PostTime.Day + tmpr.PostTime.Hour + tmpr.PostTime.Minute + tmpr.PostTime.Second + tmpr.PostTime.Millisecond + ".jpg";
            else
                tmpr.Images = "";

            uploadRecord(tmpr);

            //显示留言成功，关闭此childwindow
            childwin.Close();
        }



        /// <summary>
        /// 同步到数据库
        /// 留言和照片都是备选项
        /// </summary>
        /// <param name="cpr"></param>
        private void uploadRecord(ServiceReference1.CPollutedRecord cpr)
        {
            //数据库部分
            addRecordClient.insertRecordCompleted += new EventHandler<imaginecup.ServiceReference1.insertRecordCompletedEventArgs>(addRecordClient_insertRecordCompleted);
            addRecordClient.insertRecordAsync(cpr);

            //图片数据上传
            if( imageupload )
            {
                WebClient uploadimageclient = new WebClient();
                Uri uri = new Uri(uploadservice + cpr.Images, UriKind.Absolute);
                uploadimageclient.OpenWriteCompleted += new OpenWriteCompletedEventHandler(uploadimageclient_OpenWriteCompleted);
                uploadimageclient.OpenWriteAsync(uri, "POST", openFile.File.OpenRead());
            }
        }

        void addRecordClient_insertRecordCompleted(object sender, imaginecup.ServiceReference1.insertRecordCompletedEventArgs e)
        {
            //do nothing
            bool testbool = (bool)(e.Result);
            ((AddRecordWin)(this.Parent)).Close();
        }

        void uploadimageclient_OpenWriteCompleted(object sender, OpenWriteCompletedEventArgs e)
        {
            Stream inputStream = e.UserState as Stream;
            Stream outputStream = e.Result;
            byte[] buffer = new byte[4096];
            int byteRead = 0;
            while((byteRead = inputStream.Read(buffer,0,buffer.Length)) >0)
            {
                outputStream.Write(buffer,0,byteRead);
            }

            outputStream.Close();
            inputStream.Close();
        }

        
        
    }
}
